#include <stdint.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include "../../lv_demos/lv_demo.h"
#include "ft_main.h"

//static lv_obj_t *scr_main=NULL;
static lv_obj_t *win_main = NULL;
static lv_obj_t *tab_main = NULL;
static lv_obj_t * btnm1 = NULL;

static lv_obj_t *wifi_label = NULL;
static lv_obj_t *bgk = NULL;
static lv_obj_t *wifi_switch = NULL;
static lv_obj_t *wifi_list = NULL;

static lv_obj_t *info_status_label = NULL;
static lv_obj_t *info_ap_label = NULL;
static lv_obj_t *info_ip_label = NULL;

static uint8_t password[64] = { 0 };
static lv_obj_t *ta = NULL;
static lv_obj_t *kb = NULL;

static void event_handler(lv_event_t *event);
static void wifi_password_close(void);
static void wifi_list_clear(void);
static const char * btnm_map[] = { 
 "fail", "pass",
 "" };


static void ft_wifi_destory(void)
{
	printf("ft_wifi_destory + \n");
	if (win_main != NULL) {
		lv_obj_del(win_main);
		win_main = NULL;
		btnm1 = NULL;
	}

}

static void wifi_onoff_completed(lv_timer_t *tmr)
{
	(void)tmr;
	printf("wifi_onoff_completed \n");
	if (wifi_switch != NULL) {
		lv_obj_clear_state(wifi_switch,LV_STATE_DISABLED);
	}

}

static void wifi_on(void) 
{

}

static void wifi_off(void) 
{
	wifi_list_clear();
	lv_label_set_text(info_ap_label, "NONE");
	lv_label_set_text(info_status_label, "Disconnect");
}

static void wifi_scan(void)
{

}

static void wifi_connect(char* ssid,char* password)
{

}

static void get_input_password(void)
{
	char* pw = lv_textarea_get_text(ta);
	printf("pw=%s,len=%d\n", pw, strlen(pw));
}

static void ta_event_cb(lv_event_t * e)
{
	lv_event_code_t code = lv_event_get_code(e);
	lv_obj_t * ta = lv_event_get_target(e);
	lv_obj_t * kb = lv_event_get_user_data(e);
	if (code < 18 || (code > 27 && code < 33)) {
		printf("wzb kb_event_cb code=%d\n", code);
	}
	if (code == LV_EVENT_FOCUSED) {
		lv_keyboard_set_textarea(kb, ta);
		lv_obj_clear_flag(kb, LV_OBJ_FLAG_HIDDEN);
	}

	else if (code == LV_EVENT_DEFOCUSED) {
		lv_keyboard_set_textarea(kb, NULL);
		lv_obj_add_flag(kb, LV_OBJ_FLAG_HIDDEN);
	}
	else if (code == LV_EVENT_CANCEL || code == LV_EVENT_READY) {
		get_input_password();
		wifi_password_close();
		lv_tabview_set_act(tab_main, 1, LV_ANIM_OFF);
		lv_label_set_text(info_status_label, "connecting");
	}
}

static void wifi_password_editbox(void)
{
	printf("wifi_password_editbox + \n");
	if(kb==NULL)kb = lv_keyboard_create(bgk);
	//lv_obj_add_flag(kb, LV_OBJ_FLAG_HIDDEN);
	if (ta == NULL)ta = lv_textarea_create(bgk);
	lv_obj_set_size(ta, 200, 150);
	//lv_obj_set_pos(ta, 10, 0);
	//lv_obj_set_size(kb, 200, 240);
	//lv_obj_set_pos(kb, 0, 80);
	lv_obj_add_event_cb(ta, ta_event_cb, LV_EVENT_ALL, kb);
	lv_keyboard_set_textarea(kb, ta);
}

static void wifi_password_close(void)
{
	if (ta != NULL) {
		lv_obj_del(ta);
		ta = NULL;
	}
	if (kb != NULL) {
		lv_obj_del(kb);
		kb = NULL;
	}
}

static void wifi_list_clear(void)
{
	if (wifi_list == NULL) {
		return;
	}
	lv_obj_clean(wifi_list);
}
static uint8_t wifi_get_ap_count(void)
{
	return 8;
}

static void wifi_list_test(void)
{
	if (wifi_list == NULL) {
		return;
	}
	uint8_t i = 0;
	lv_obj_t * list_btn;
	list_btn = lv_list_add_btn(wifi_list, LV_SYMBOL_REFRESH, "Scan");
	lv_obj_add_event_cb(list_btn, event_handler, LV_EVENT_CLICKED, (void *)0xF1);
	for (i = 0; i < wifi_get_ap_count(); i++) {
		list_btn = lv_list_add_btn(wifi_list, LV_SYMBOL_WIFI, "TPLINK   -56");
		lv_obj_add_event_cb(list_btn, event_handler, LV_EVENT_CLICKED, (void *)i);
	}
}
static void wifi_list_clean(void)
{
	if (wifi_list == NULL) {
		return;
	}
	lv_obj_clean(wifi_list);
}


static void wifi_list_update(void)
{
	if (wifi_list == NULL) {
		return;
	}
	lv_obj_clean(wifi_list);
}

static uint8_t  wifi_get_state(void) 
{
	uint8_t state = 0;
	return state;
}


static void event_handler(lv_event_t *event)
{
	uint32_t data = 0;
	data = (uint32_t)lv_event_get_user_data(event);

	if (event->code == LV_EVENT_VALUE_CHANGED) {
		printf("[Ft_wifi]Clicked: data=%ld\n", data);

		switch (data)
		{

		case 0xa1:
		{
			
			lv_obj_t * obj = lv_event_get_target(event);
			uint16_t index = lv_btnmatrix_get_selected_btn(obj);
			if (index == 0) {
				lv_btnmatrix_set_btn_ctrl(btnm1, index, LV_BTNMATRIX_CTRL_CHECKED);
			
				g_item_result[11] = 0;
				ft_main_refresh_list((void*)0x0b);
				ft_wifi_destory();
			
				
			}
			else if (index == 1) {
				lv_btnmatrix_set_btn_ctrl(btnm1, index, LV_BTNMATRIX_CTRL_CHECKED);
				g_item_result[11] = 1;
				ft_main_refresh_list((void*)0x0b);
				ft_wifi_destory();
				
				
			}

		}
		break;
		case 0xa2:
		{
			lv_obj_t * obj = lv_event_get_target(event);
			uint8_t wifi_state = lv_obj_get_state(obj)&LV_STATE_CHECKED;
			printf("wifi switch:%d\n", wifi_state);
			lv_obj_add_state(obj,LV_STATE_DISABLED);
			if (wifi_state) {
				wifi_on();
			}
			else {
				wifi_off();
			}
			lv_timer_t *task1 = lv_timer_create(wifi_onoff_completed, 3000, NULL);
			lv_timer_set_repeat_count(task1, 1);
			break;
		}
		default:
			break;
		}
	}
	else if (event->code == LV_EVENT_CLICKED) {
		printf("LV_EVENT_CLICKED data=%d\n", data);
		if (data == 0xf1) {
			//scan
			wifi_scan();
		}
		else {
		
			//if (data < WIFI_SCAN_LIST_SIZE) {
			//	wifi_connect(test_g_ap_list[data].ssid,password);
			//}
			wifi_password_editbox();
		}
	}
}





static void ft_wifi_init_view(void)
{
	printf("ft_wifi_init_view + \n");
	//scr_main = lv_obj_create(NULL);
	//lv_scr_load(scr_main);
	win_main = lv_win_create(lv_scr_act(), 0);
	lv_obj_set_size(win_main, 240, 320);

	tab_main = lv_tabview_create(win_main, LV_DIR_TOP, LV_DPI_DEF / 3);
	lv_obj_t *tab1 = lv_tabview_add_tab(tab_main,"WiFi");
	lv_obj_t *tab2 = lv_tabview_add_tab(tab_main, "Info");

	
	lv_obj_t *info_mac = lv_label_create(tab2);
	lv_obj_set_size(info_mac, 80, 60);
	lv_obj_set_pos(info_mac, 10, 0);
	lv_label_set_text(info_mac, "Mac:");

	lv_obj_t *info_mac_label = lv_label_create(tab2);
	lv_obj_set_size(info_mac_label, 120, 60);
	lv_obj_set_pos(info_mac_label, 100, 0);
	lv_label_set_text(info_mac_label, "xx:xx:xx:xx:xx");


	lv_obj_t *info_status= lv_label_create(tab2);
	lv_obj_set_size(info_status, 80, 60);
	lv_obj_set_pos(info_status, 10, 70);
	lv_label_set_text(info_status, "Status");

	info_status_label = lv_label_create(tab2);
	lv_obj_set_size(info_status_label, 120, 60);
	lv_obj_set_pos(info_status_label, 100, 70);
	lv_label_set_text(info_status_label, "Disconnect");


	lv_obj_t *info_ap = lv_label_create(tab2);
	lv_obj_set_size(info_ap, 80, 60);
	lv_obj_set_pos(info_ap, 10, 140);
	lv_label_set_text(info_ap, "AP:");
	info_ap_label = lv_label_create(tab2);
	lv_obj_set_size(info_ap_label, 100, 60);
	lv_obj_set_pos(info_ap_label, 100, 140);
	lv_label_set_text(info_ap_label, "NONE");

	lv_obj_t *info_ip = lv_label_create(tab2);
	lv_obj_set_size(info_ip, 80, 60);
	lv_obj_set_pos(info_ip, 10, 210);
	lv_label_set_text(info_ip, "IP:");
	info_ip_label = lv_label_create(tab2);
	lv_obj_set_size(info_ip_label, 100, 60);
	lv_obj_set_pos(info_ip_label, 100, 210);
	lv_label_set_text(info_ip_label, "");

	bgk = lv_obj_create(tab1);
	lv_obj_set_size(bgk, 240, 320);

	wifi_label = lv_label_create(bgk);
	lv_obj_set_size(wifi_label, 80, 40);
	lv_obj_set_pos(wifi_label, 0, 0);
	lv_label_set_text(wifi_label, "WiFi");

	wifi_switch = lv_switch_create(bgk);
	lv_obj_set_size(wifi_switch, 60, 30);
	lv_obj_set_pos(wifi_switch, 140, 0);
	lv_obj_add_event_cb(wifi_switch, event_handler, LV_EVENT_VALUE_CHANGED, (void *)0xa2);

	wifi_list = lv_list_create(bgk);
	lv_obj_set_size(wifi_list, 180, 160);
	lv_obj_set_pos(wifi_list, 0, 40);


	btnm1 = lv_btnmatrix_create(bgk);
	lv_obj_set_size(btnm1, 180, 80);
	
	lv_btnmatrix_set_map(btnm1, btnm_map);

	//lv_obj_align(btnm1, LV_ALIGN_BOTTOM_MID, 0, 200);
	lv_obj_set_pos(btnm1, 0, 200);
	lv_obj_add_event_cb(btnm1, event_handler, LV_EVENT_VALUE_CHANGED, (void *)0xa1);

	if (wifi_get_state()) {
		lv_obj_add_state(wifi_switch,LV_STATE_CHECKED);
	}

	wifi_list_test();

}


void ft_wifi_create(void)
{
	printf("ft_wifi_create + \n");
	ft_wifi_init_view();

}